use "$cleaned/all_s4_cleaned.dta", clear

**************************************
*	table 1 
**************************************

global balance AGE female subject_count std_AGG_mock std_MTC_mock std_ENG_mock
reg treatment  $balance school*
test AGE female 
*can't reject 0


eststo placebo:  estpost summarize ///
    $balance if treatment == 0
eststo treatment:  estpost summarize ///
    $balance if treatment == 1
eststo diff:  estpost ttest ///
   $balance, by(treatment) unequal

   esttab  placebo treatment diff using "$output/table1a.tex", cells("mean(pattern(1 1 0) fmt(2)) sd(pattern(1 1 0)) b(star pattern(0 0 1) fmt(2))  p(pattern(0 0 1) par fmt(2))") label replace


**************************************
*	table 2 
**************************************
gen std_MTC_mock_miss =0
replace std_MTC_mock_miss=1 if std_MTC_mock==.
replace std_MTC_mock=std_AGG_mock if std_MTC_mock==.
gen std_ENG_mock_miss =0
replace std_ENG_mock_miss=1 if std_ENG_mock==.
replace std_ENG_mock=std_AGG_mock if std_ENG_mock==.

reg std_total_score2 treatment school* std_AGG_mock, vce(robust)
outreg2 using "${output}/table2.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_total_score2 treatment std_AGG_mock) replace
reg std_total_score2 treatment school* ${controls} std_AGG_mock , vce(robust)
outreg2 using "${output}/table2.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_total_score2 treatment std_AGG_mock ${controls}) 

reg std_core_score2 treatment school* std_AGG_mock, vce(robust)
outreg2 using "${output}/table2.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_core_score2 treatment std_AGG_mock ) 
reg std_core_score2 treatment school* ${controls} std_AGG_mock  , vce(robust)
outreg2 using "${output}/table2.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_core_score2 treatment std_AGG_mock ${controls}) 

reg std_MTC treatment school* std_MTC_mock  std_MTC_mock_miss, vce(robust)
outreg2 using "${output}/table2.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_MTC treatment std_MTC_mock  std_MTC_mock_miss ) 
reg std_MTC treatment school* ${controls} std_MTC_mock  std_MTC_mock_miss
outreg2 using "${output}/table2.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_MTC treatment std_MTC_mock  std_MTC_mock_miss ${controls}) 

reg MTC_1 treatment school* std_MTC_mock  std_MTC_mock_miss, vce(robust)
outreg2 using "${output}/table2.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(MTC_1 treatment std_MTC_mock  std_MTC_mock_miss ) 
reg MTC_1 treatment school* ${controls} std_MTC_mock  std_MTC_mock_miss
outreg2 using "${output}/table2.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(MTC_1 treatment std_MTC_mock  std_MTC_mock_miss ${controls}) 

reg std_ENG treatment school* std_ENG_mock std_ENG_mock_miss, vce(robust)
outreg2 using "${output}/table2.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_ENG treatment std_ENG_mock std_ENG_mock_miss ) 
reg std_ENG treatment school* ${controls} std_ENG_mock std_ENG_mock_miss
outreg2 using "${output}/table2.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_ENG treatment std_ENG_mock std_ENG_mock_miss ${controls}) 

reg ENG_1 treatment school* std_ENG_mock std_ENG_mock_miss, vce(robust)
outreg2 using "${output}/table2.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(ENG_1 treatment std_ENG_mock std_ENG_mock_miss ) 
reg ENG_1 treatment school* ${controls} std_ENG_mock std_ENG_mock_miss
outreg2 using "${output}/table2.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(ENG_1 treatment std_ENG_mock std_ENG_mock_miss ${controls}) 


**************************************
*	table 4
**************************************
gen below_female=female*below_median_mock
global subgroup female below_median_mock  
global subgroup2 female below_median_mock  below_female

foreach x of global subgroup2 { 
capture gen treatment_`x'=treatment*`x'
local label: var la `x'
la var treatment_`x' "Treatment * `label'"
}

preserve 
local i=1
tempfile tf`i' 
reg std_MTC treatment school* treatment_female ${controls} std_MTC_mock
parmest, format(p %12.9f) saving(`tf`i'',replace) idn(`i')
local i=`i'+1
tempfile tf`i' 
reg MTC_1 treatment school* treatment_female ${controls} std_MTC_mock
parmest, format(p %12.9f) saving(`tf`i'',replace) idn(`i')

local i=`i'+1
tempfile tf`i' 
reg std_MTC treatment school* treatment_below_median_mock below_median_mock ${controls} std_MTC_mock
parmest, format(p %12.9f) saving(`tf`i'',replace) idn(`i')
local i=`i'+1
tempfile tf`i' 
reg MTC_1 treatment school* treatment_below_median_mock below_median_mock ${controls} std_MTC_mock
parmest, format(p %12.9f) saving(`tf`i'',replace) idn(`i')

local i=`i'+1
tempfile tf`i' 
reg std_MTC treatment school* treatment_below_female treatment_below_median_mock below_median_mock treatment_female  ${controls} std_MTC_mock
parmest, format(p %12.9f) saving(`tf`i'',replace) idn(`i')
local i=`i'+1
tempfile tf`i' 
reg MTC_1 treatment school* treatment_below_female treatment_below_median_mock below_median_mock treatment_female  ${controls} std_MTC_mock
parmest, format(p %12.9f) saving(`tf`i'',replace) idn(`i')


dsconcat  `tf1'
keep if parm=="treatment"  | parm=="treatment_female"
keep p idnum
qqvalue p, method(yekutieli) qvalue(q)
replace q=0.99 if q==1
format q %8.2f
replace q=round(q, 0.01)
*qqvalue p, method(bonferroni) qvalue(q2)
foreach x of numlist 1/2 {
global Tq1`x'=q[`x']
display ${Tq1`x'}
}



dsconcat  `tf3'
keep if parm=="treatment" | parm=="treatment_below_median_mock" 
keep p idnum
qqvalue p, method(yekutieli) qvalue(q)
replace q=0.99 if q==1
format q %8.2f
replace q=round(q, 0.01)
*qqvalue p, method(bonferroni) qvalue(q2)
foreach x of numlist 1/2 {
global Tq3`x'=q[`x']
display ${Tq3`x'}
}


dsconcat  `tf5'  
keep if parm=="treatment" | parm=="treatment_below_female" | parm=="treatment_below_median_mock" | parm=="treatment_female"
keep p idnum
qqvalue p, method(yekutieli) qvalue(q)
replace q=0.99 if q==1
format q %8.2f
replace q=round(q, 0.01)
*qqvalue p, method(bonferroni) qvalue(q2)
foreach x of numlist 1/4 {
global Tq5`x'=q[`x']
display ${Tq5`x'}
}


dsconcat  `tf2'
keep if parm=="treatment"  | parm=="treatment_female"
keep p idnum
qqvalue p, method(yekutieli) qvalue(q)
replace q=0.99 if q==1
format q %8.2f
replace q=round(q, 0.01)
*qqvalue p, method(bonferroni) qvalue(q2)
foreach x of numlist 1/2 {
global Tq2`x'=q[`x']
display ${Tq2`x'}
}


dsconcat  `tf4'
keep if parm=="treatment" | parm=="treatment_below_median_mock" 
keep p idnum
qqvalue p, method(yekutieli) qvalue(q)
replace q=0.99 if q==1
format q %8.2f
replace q=round(q, 0.01)
*qqvalue p, method(bonferroni) qvalue(q2)
foreach x of numlist 1/2 {
global Tq4`x'=q[`x']
display ${Tq4`x'}
}


dsconcat  `tf6'  
keep if parm=="treatment" | parm=="treatment_below_female" | parm=="treatment_below_median_mock" | parm=="treatment_female"
keep p idnum
qqvalue p, method(yekutieli) qvalue(q)
replace q=0.99 if q==1
format q %8.2f
replace q=round(q, 0.01)
*qqvalue p, method(bonferroni) qvalue(q2)
foreach x of numlist 1/4 {
global Tq6`x'=q[`x']
display ${Tq6`x'}
}

restore

reg std_MTC treatment school* treatment_female  ${controls} std_MTC_mock
test female + treatment_female + treatment = female
lincom treatment_female + treatment
local F1c = r(estimate)
local F1se = r(se)
test female + treatment_female + treatment = female
local p_value = r(p)
outreg2 using "${output}/table4.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_MTC_mock MTC treatment treatment_female  ${controls} ) addstat(treated, `F1c', SE , `F1se', p_value, `p_value', qvalue interaction f, ${Tq12}, qvalue, ${Tq11}) replace adec(2)

reg MTC_1 treatment school* treatment_female  ${controls} std_MTC_mock
lincom treatment_female + treatment
local F1c = r(estimate)
local F1se = r(se)
test female + treatment_female + treatment = female
local p_value = r(p)
outreg2 using "${output}/table4.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_MTC_mock MTC treatment treatment_female ${controls} ) addstat(treated, `F1c', SE , `F1se', p_value, `p_value', qvalue interaction f, ${Tq22}, qvalue, ${Tq21}) adec(2)


reg std_MTC treatment school* treatment_below_median_mock below_median_mock  ${controls} std_MTC_mock
test below_median_mock + treatment_below_median_mock + treatment = below_median_mock
lincom treatment_below_median_mock + treatment
local F1c = r(estimate)
local F1se = r(se)
test below_median_mock + treatment_below_median_mock + treatment = below_median_mock
local p_value = r(p)
outreg2 using "${output}/table4.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_MTC_mock MTC treatment treatment_below_median_mock below_median_mock ${controls} ) addstat(treated, `F1c', SE , `F1se', p_value, `p_value', qvalue interaction b, ${Tq32}, qvalue, ${Tq31}) adec(2)

reg MTC_1 treatment school* treatment_below_median_mock below_median_mock  ${controls} std_MTC_mock
test below_median_mock + treatment_below_median_mock + treatment = below_median_mock
lincom treatment_below_median_mock + treatment
local F1c = r(estimate)
local F1se = r(se)
test below_median_mock + treatment_below_median_mock + treatment = below_median_mock
local p_value = r(p)
outreg2 using "${output}/table4.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_MTC_mock MTC treatment treatment_below_median_mock below_median_mock ${controls} ) addstat(treated, `F1c', SE , `F1se', p_value, `p_value', qvalue interaction b, ${Tq42}, qvalue, ${Tq41}) adec(2)

reg std_MTC treatment school* treatment_female treatment_below_female treatment_below_median_mock below_median_mock ${controls} std_MTC_mock, vce(robust)
lincom treatment+ treatment_female // impact for high ability women
local F1c = r(estimate)
local F1se = r(se)
test treatment+ treatment_female = 0
local p_value1 = r(p)
lincom treatment+ treatment_female + treatment_below_median_mock + treatment_below_female // impact for low ability women
local F2c = r(estimate)
local F2se = r(se)
test treatment+ treatment_female + treatment_below_median_mock + treatment_below_female = 0
local p_value2 = r(p)
lincom treatment // impact for high ability men
local F3c = r(estimate)
local F3se = r(se)
test treatment = 0
local p_value3 = r(p)
lincom treatment + treatment_below_median_mock //impact for low ability men
local F4c = r(estimate)
local F4se = r(se)
test treatment+ treatment_below_median_mock = 0
local p_value4 = r(p)
outreg2 using "${output}/table4.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_MTC_mock  treatment treatment_female below_median_mock treatment_below_female treatment_below_median_mock ${controls} ) ///
 addstat(high ability women, `F1c', SE , `F1se', p_value, `p_value1', low ability women, `F2c', SE , `F2se', p_value, `p_value2', high ability men, `F3c', SE , `F3se', p_value, `p_value3', low ability men, `F4c', SE , `F4se', p_value, `p_value4', qvalue interaction f, ${Tq54}, qvalue, ${Tq51}, qvalue interaction b, ${Tq53},  qvalue interaction fb, ${Tq52}) adec(2)

reg MTC_1 treatment school* treatment_female treatment_below_female treatment_below_median_mock below_median_mock ${controls} std_MTC_mock, vce(robust)
lincom treatment+ treatment_female // impact for high ability women
local F1c = r(estimate)
local F1se = r(se)
test treatment+ treatment_female = 0
local p_value1 = r(p)
lincom treatment+ treatment_female + treatment_below_median_mock + treatment_below_female // impact for low ability women
local F2c = r(estimate)
local F2se = r(se)
test treatment+ treatment_female + treatment_below_median_mock + treatment_below_female = 0
local p_value2 = r(p)
lincom treatment // impact for high ability men
local F3c = r(estimate)
local F3se = r(se)
test treatment = 0
local p_value3 = r(p)
lincom treatment + treatment_below_median_mock //impact for low ability men
local F4c = r(estimate)
local F4se = r(se)
test treatment+ treatment_below_median_mock = 0
local p_value4 = r(p)
outreg2 using "${output}/table4.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_MTC_mock  treatment treatment_female below_median_mock treatment_below_female treatment_below_median_mock ${controls} ) ///
addstat(high ability women, `F1c', SE , `F1se', p_value, `p_value1', low ability women, `F2c', SE , `F2se', p_value, `p_value2', high ability men, `F3c', SE , `F3se', p_value, `p_value3', low ability men, `F4c', SE , `F4se', p_value, `p_value4', qvalue interaction f, ${Tq64}, qvalue, ${Tq61}, qvalue interaction b, ${Tq63}, qvalue interaction fb, ${Tq62}) adec(2)




**************************************
*	Figure A2
**************************************

//adjusted scores
reg std_total_score2  school* ${controls} std_AGG_mock , vce(robust)
predict covariates_effect_total
gen adjusted_score_total=std_total_score2-covariates_effect_total
twoway (kdensity adjusted_score_total if treatment==1,  color(gray)) (kdensity adjusted_score_total if treatment==0, fcolor(none) lcolor(black)), legend(order(1 "Treated" 2 "Placebo" )) ytitle("Kdensity Total score") ylabel(, grid)
graph export "${output}/fig2_density_total.png", as(png) replace
ksmirnov adjusted_score_total, by(treatment)

reg std_core_score2  school* ${controls} std_AGG_mock , vce(robust)
predict covariates_effect_core
gen adjusted_score_core=std_core_score2-covariates_effect_core
twoway (kdensity adjusted_score_core if treatment==1,  color(gray)) (kdensity adjusted_score_core if treatment==0, fcolor(none) lcolor(black)), legend(order(1 "Treated" 2 "Placebo" )) ytitle("Kdensity Core score") ylabel(, grid)
graph export "${output}/fig2_density_core.png", as(png) replace
ksmirnov adjusted_score_core, by(treatment)

reg std_MTC  school* ${controls} std_MTC_mock, vce(robust)
predict covariates_effect_MTC
gen adjusted_score_MTC=std_MTC-covariates_effect_MTC
twoway (kdensity adjusted_score_MTC if treatment==1,  color(gray)) (kdensity adjusted_score_MTC if treatment==0, fcolor(none) lcolor(black)), legend(order(1 "Treated" 2 "Placebo" )) ytitle("Kdensity Maths") ylabel(, grid)
graph export "${output}/fig2_density_MTC.png", as(png) replace
ksmirnov adjusted_score_MTC, by(treatment)

reg std_ENG  school* ${controls} std_ENG_mock, vce(robust)
predict covariates_effect_ENG
gen adjusted_score_ENG=std_ENG-covariates_effect_ENG
twoway (kdensity adjusted_score_ENG if treatment==1,  color(gray)) (kdensity adjusted_score_ENG if treatment==0, fcolor(none) lcolor(black)), legend(order(1 "Treated" 2 "Placebo" )) ytitle("Kdensity English")  ylabel(, grid)
graph export "${output}/fig2_density_ENG.png", as(png) replace
ksmirnov adjusted_score_ENG, by(treatment)


**************************************
*	Table A5
**************************************


ologit MTC treatment school1-school7 ${controls} std_MTC_mock , vce(robust)
 margins,  dydx(treatment)  post
outreg2 using "${output}/table_A5.tex",  dec(3)  title(Ordered logit regression of the impact of treatment on maths grade at S4) replace

ologit ENG treatment school1-school7 ${controls} std_ENG_mock, vce(robust)
 margins,  dydx(treatment)  post
outreg2 using "${output}/table_A5.tex",  dec(3)  title(Ordered logit regression of the impact of treatment on core subject grade for S4) 

ologit CHEM treatment school1-school7 ${controls} std_AGG_mock, vce(robust)
 margins,  dydx(treatment)  post
outreg2 using "${output}/table_A5.tex",  dec(3)  

ologit PHY treatment school1-school7 ${controls} std_AGG_mock, vce(robust)
 margins,  dydx(treatment)  post
outreg2 using "${output}/table_A5.tex",  dec(3)  

ologit BIO treatment school1-school7 ${controls} std_AGG_mock, vce(robust)
margins,  dydx(treatment)  post
outreg2 using "${output}/table_A5.tex",  dec(3) 

ologit HIST treatment school1-school7 ${controls} std_AGG_mock, vce(robust)
 margins,  dydx(treatment)  post
outreg2 using "${output}/table_A5.tex",  dec(3) 

ologit GEOG treatment school1-school7 ${controls} std_AGG_mock, vce(robust)
 margins,  dydx(treatment)  post
outreg2 using "${output}/table_A5.tex",  dec(3)  

**************************************
*	Table A6
**************************************

reg std_total_score_exm treatment school1-school7 std_AGG_mock,  vce(robust)
outreg2 using "${output}/table_A6.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_total_score_exm treatment ${controls} std_AGG_mock) replace
reg std_total_score_exm treatment school1-school7 ${controls} std_AGG_mock,  vce(robust)
outreg2 using "${output}/table_A6.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_total_score_exm treatment ${controls} std_AGG_mock) 


**************************************
*	Table A8
**************************************

preserve 
foreach y of global subgroup  {
local i=1
tempfile tf`y'`i' 
reg std_total_score2 treatment school* treatment_`y' `y' ${controls} std_AGG_mock
parmest, format(p %12.9f) saving(`tf`y'`i'',replace) idn(`i')
local i=`i'+1
tempfile tf`y'`i' 
reg std_core_score2 treatment school* treatment_`y' `y' ${controls} std_AGG_mock
parmest, format(p %12.9f) saving(`tf`y'`i'',replace) idn(`i')
local i=`i'+1
tempfile tf`y'`i' 
reg std_MTC treatment school* treatment_`y' `y' ${controls} std_MTC_mock
parmest, format(p %12.9f) saving(`tf`y'`i'',replace) idn(`i')
local i=`i'+1
tempfile tf`y'`i' 
reg std_ENG treatment school* treatment_`y' `y' ${controls} std_ENG_mock
parmest, format(p %12.9f) saving(`tf`y'`i'',replace) idn(`i')
local i=`i'+1
tempfile tf`y'`i' 
reg MTC_1 treatment school* treatment_`y' `y' ${controls} std_MTC_mock
parmest, format(p %12.9f) saving(`tf`y'`i'',replace) idn(`i')

}

local j=1
foreach y of global subgroup  {
dsconcat `tf`y'1' `tf`y'2' `tf`y'3'  `tf`y'4'  `tf`y'5'
keep if parm=="treatment_`y'"
keep p idnum
qqvalue p, method(yekutieli) qvalue(q)
replace q=0.99 if q==1
format q %12.0g
foreach x of numlist 1/5 {
global Tbq`j'`x'=q[`x']
display ${Tbq`j'`x'}
}

dsconcat `tf`y'1' `tf`y'2' `tf`y'3'  `tf`y'4'  `tf`y'5'
keep if parm=="treatment"
keep p idnum
qqvalue p, method(yekutieli) qvalue(q)
replace q=0.99 if q==1
format q %12.0g
foreach x of numlist 1/5 {
global Tq`j'`x'=q[`x']
display ${Tq`j'`x'}
}
local j=`j'+1
}

restore


local i=1
foreach x of global subgroup { 
reg std_total_score2 treatment school* treatment_`x' `x' ${controls} std_AGG_mock
test `x' + treatment_`x' + treatment = `x'
lincom treatment_`x' + treatment
local F1c = r(estimate)
local F1se = r(se)
test `x' + treatment_`x' + treatment = `x'
local p_value = r(p)
if `i'==1 {
outreg2 using "${output}/table_A8.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_AGG_mock std_total_score2 treatment treatment_`x' `x' ${controls} )  addstat(treated, `F1c',SE , `F1se', p_value, `p_value', qvalue interaction, ${Tbq`i'1}, qvalue, ${Tq`i'1} ) replace adec(2)
}
else {
outreg2 using "${output}/table_A8.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_AGG_mock std_total_score2 treatment treatment_`x' `x' ${controls} )  addstat(treated, `F1c',SE , `F1se', p_value,  `p_value', qvalue interaction, ${Tbq`i'1}, qvalue, ${Tq`i'1}) adec(2)

}
reg std_core_score2 treatment school* treatment_`x' `x' ${controls} std_AGG_mock
test `x' + treatment_`x' + treatment = `x'
lincom treatment_`x' + treatment
local F1c = r(estimate)
local F1se = r(se)
test `x' + treatment_`x' + treatment = `x'
local p_value = r(p)
outreg2 using "${output}/table_A8.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_AGG_mock std_core_score2 treatment treatment_`x' `x' ${controls} )  addstat(treated, `F1c',SE , `F1se', p_value, `p_value', qvalue interaction, ${Tbq`i'2}, qvalue, ${Tq`i'2}) adec(2)
reg std_MTC treatment school* treatment_`x' `x' ${controls} std_MTC_mock
test `x' + treatment_`x' + treatment = `x'
lincom treatment_`x' + treatment
local F1c = r(estimate)
local F1se = r(se)
test `x' + treatment_`x' + treatment = `x'
local p_value = r(p)
outreg2 using "${output}/table_A8.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_MTC_mock MTC treatment treatment_`x' `x' ${controls} )  addstat(treated, `F1c',SE , `F1se', p_value, `p_value', qvalue interaction, ${Tbq`i'3}, qvalue, ${Tq`i'3}) adec(2)

reg std_ENG treatment school* treatment_`x' `x' ${controls} std_ENG_mock
test `x' + treatment_`x' + treatment = `x'
lincom treatment_`x' + treatment
local F1c = r(estimate)
local F1se = r(se)
test `x' + treatment_`x' + treatment = `x'
local p_value = r(p)
outreg2 using "${output}/table_A8.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_ENG_mock std_ENG treatment treatment_`x' `x' ${controls} )  addstat(treated, `F1c',SE , `F1se', p_value, `p_value', qvalue interaction, ${Tbq`i'4}, qvalue, ${Tq`i'4}) adec(2)

reg MTC_1 treatment school* treatment_`x' `x' ${controls} std_MTC_mock
lincom treatment_`x' + treatment
local F1c = r(estimate)
local F1se = r(se)
test `x' + treatment_`x' + treatment = `x'
local p_value = r(p)
outreg2 using "${output}/table_A8.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_MTC_mock MTC_1 treatment treatment_`x' `x' ${controls} )  addstat(treated, `F1c',SE , `F1se', p_value, `p_value', qvalue interaction, ${Tbq`i'5}, qvalue, ${Tq`i'5}) adec(2)
local i=`i'+1
display `i'
}


**************************************
*	Table A9
**************************************

//by mock performance deciles
xtile decile_mock=AGG_mock , nq(10)
tab decile_mock, gen(decile_mock_)
foreach x of varlist decile_mock* {
gen treatment_`x'=treatment*`x'
}

reg std_MTC treatment treatment_decile_mock_2-treatment_decile_mock_10 school* decile_mock_2-decile_mock_10 $controls std_MTC_mock, vce(robust)
foreach x of varlist decile_mock_2 - decile_mock_10 {
lincom treatment_`x' + treatment
local F1c`x' = r(estimate)
local F1se`x' = r(se)
test  treatment_`x' + treatment = 0
local p_value`x'=r(p)
}
outreg2 using  "${output}/table_A9.tex",  dec(2) adec(2) addstat(decile 2, `F1cdecile_mock_2', se2, `F1sedecile_mock_2', p_value1, `p_valuedecile_mock_2',decile 3, `F1cdecile_mock_3',se3  , `F1sedecile_mock_3', p_value3, `p_valuedecile_mock_3',decile 4, `F1cdecile_mock_4',se4  , `F1sedecile_mock_4', p_value4, `p_valuedecile_mock_4',decile 5, `F1cdecile_mock_5',se5  , `F1sedecile_mock_5', p_value5, `p_valuedecile_mock_5',decile 6, `F1cdecile_mock_6', se6 , `F1sedecile_mock_6', p_value6, `p_valuedecile_mock_6',decile 7, `F1cdecile_mock_7', se7 , `F1sedecile_mock_7', p_value7, `p_valuedecile_mock_7',decile 8, `F1cdecile_mock_8', se8 , `F1sedecile_mock_8', p_value8, `p_valuedecile_mock_8',decile 9, `F1cdecile_mock_9', se9 , `F1sedecile_mock_9', p_value9, `p_valuedecile_mock_9',decile 10, `F1cdecile_mock_10',se10  , `F1sedecile_mock_10', p_value10, `p_valuedecile_mock_10' ) replace

reg MTC_1 treatment treatment_decile_mock_2-treatment_decile_mock_10 school* decile_mock_2-decile_mock_10 $controls std_MTC_mock , vce(robust)
foreach x of varlist decile_mock_2 - decile_mock_10 {
lincom treatment_`x' + treatment
local F1c`x' = r(estimate)
local F1se`x' = r(se)
test  treatment_`x' + treatment = 0
local p_value`x'=r(p)
}
outreg2 using  "${output}/table_A9.tex",  dec(2) adec(2) addstat(decile 2, `F1cdecile_mock_2', se2, `F1sedecile_mock_2', p_value1, `p_valuedecile_mock_2',decile 3, `F1cdecile_mock_3',se3  , `F1sedecile_mock_3', p_value3, `p_valuedecile_mock_3',decile 4, `F1cdecile_mock_4',se4  , `F1sedecile_mock_4', p_value4, `p_valuedecile_mock_4',decile 5, `F1cdecile_mock_5',se5  , `F1sedecile_mock_5', p_value5, `p_valuedecile_mock_5',decile 6, `F1cdecile_mock_6', se6 , `F1sedecile_mock_6', p_value6, `p_valuedecile_mock_6',decile 7, `F1cdecile_mock_7', se7 , `F1sedecile_mock_7', p_value7, `p_valuedecile_mock_7',decile 8, `F1cdecile_mock_8', se8 , `F1sedecile_mock_8', p_value8, `p_valuedecile_mock_8',decile 9, `F1cdecile_mock_9', se9 , `F1sedecile_mock_9', p_value9, `p_valuedecile_mock_9',decile 10, `F1cdecile_mock_10',se10  , `F1sedecile_mock_10', p_value10, `p_valuedecile_mock_10' )



**************************************
*	Table A18
**************************************

reg pass_min treatment  school*,  vce(robust)
sum pass_min if treatment==0
local mean=r(mean)
outreg2 using "${output}/table_A18.tex", dec(2) title(Impact of treatment assignment on minimum grades at S4 to do a vocational certificate) keep(pass_min treatment)  adec(2) addstat(control mean, `mean') replace

reg pass_min treatment  school* ${controls} std_AGG_mock,  vce(robust)
sum pass_min if treatment==0
local mean=r(mean)
outreg2 using "${output}/table_A18.tex", dec(2)  keep(pass_min treatment ${controls} std_AGG_mock)  adec(2) addstat(control mean, `mean')

reg pass_min treatment treatment_female  school* ${controls} std_AGG_mock,  vce(robust)
sum pass_min if treatment==0 & female==1
local mean=r(mean)
outreg2 using "${output}/table_A18.tex", dec(2)  keep(pass_min treatment treatment_female ${controls} std_AGG_mock)  adec(2) addstat(control mean female, `mean')



**************************************
*	Table A20
**************************************
//school char
local subgroup  top_500   high_fees
foreach x of local subgroup { 
cap gen treatment_`x'=treatment*`x'
}

preserve 
foreach y of local subgroup  {
local i=1
tempfile tf`y'`i' 
reg std_MTC treatment school* treatment_`y' `y' ${controls} std_MTC_mock
parmest, format(p %12.9f) saving(`tf`y'`i'',replace) idn(`i')
local i=`i'+1
tempfile tf`y'`i' 
reg MTC_1 treatment school* treatment_`y' `y' ${controls} std_MTC_mock
parmest, format(p %12.9f) saving(`tf`y'`i'',replace) idn(`i')

}

local j=1
foreach y of local subgroup  {
dsconcat `tf`y'1' `tf`y'2' 
keep if parm=="treatment_`y'"
keep p idnum
qqvalue p, method(yekutieli) qvalue(q)
replace q=0.99 if q==1
format q %12.0g
*qqvalue p, method(bonferroni) qvalue(q2)
foreach x of numlist 1/2 {
global Tbq`j'`x'=q[`x']
display ${Tbq`j'`x'}
}

dsconcat `tf`y'1' `tf`y'2' 
keep if parm=="treatment"
keep p idnum
qqvalue p, method(yekutieli) qvalue(q)
replace q=0.99 if q==1
format q %12.0g
*qqvalue p, method(bonferroni) qvalue(q2)
foreach x of numlist 1/2 {
global Tq`j'`x'=q[`x']
display ${Tq`j'`x'}
}
local j=`j'+1
}

restore

local i=1
foreach x of local subgroup { 
reg std_MTC treatment school* treatment_`x' `x' ${controls} std_MTC_mock
lincom treatment_`x' + treatment
local F1c = r(estimate)
local F1se = r(se)
test  treatment_`x' + treatment = 0
local p_value=r(p)
if `i'==1 {
outreg2 using  "${output}/table_A20.tex", title(Impact of treatment assignment on standardized test scores) keep(std_total_score2 treatment treatment_`x' `x' )  dec(2) adec(2) addstat(var , `F1c', se, `F1se', p_value, `p_value',qvalue interaction, ${Tbq`i'1}, qvalue, ${Tq`i'1}) replace
}
else {
outreg2 using  "${output}/table_A20.tex", title(Impact of treatment assignment on standardized test scores) keep(std_total_score2 treatment treatment_`x' `x' )  dec(2) adec(2) addstat(var , `F1c', se, `F1se', p_value, `p_value', qvalue interaction, ${Tbq`i'1}, qvalue, ${Tq`i'1})

}
reg MTC_1 treatment school* treatment_`x' `x' ${controls} std_MTC_mock
lincom treatment_`x' + treatment
local F1c = r(estimate)
local F1se = r(se)
test  treatment_`x' + treatment = 0
local p_value=r(p)
outreg2 using  "${output}/table_A20.tex", title(Impact of treatment assignment on standardized test scores) keep(std_total_score2 treatment treatment_`x' `x' )  dec(2) adec(2) addstat(var , `F1c', se, `F1se', p_value, `p_value', qvalue interaction, ${Tbq`i'2}, qvalue, ${Tq`i'2})
local i=`i'+1
}




**************************************
*	Table A22
**************************************
*permutation tests
permute treatment _b[treatment], rep(1000): reg std_total_score2 treatment school* //0.917
permute treatment _b[treatment], rep(1000): reg std_core_score2 treatment school* //0.962
permute treatment _b[treatment], rep(1000): reg std_MTC treatment school* // 0.0380
permute treatment _b[treatment], rep(1000): reg std_ENG treatment school* //0.4930

*no controls
reg std_total_score2 treatment school* std_AGG_mock, vce(robust)
outreg2 using "${output}/table_A22.tex", dec(3) title(Impact of treatment assignment on standardized test scores) pval keep( treatment)  addstat(permutation test, 0.917) replace
reg std_core_score2 treatment school* std_AGG_mock, vce(robust)
outreg2 using "${output}/table_A22.tex", dec(3) title(Impact of treatment assignment on standardized test scores) pval keep( treatment)  addstat(permutation test, 0.962)
reg std_MTC treatment school* std_MTC_mock, vce(robust)
outreg2 using "${output}/table_A22.tex", dec(3) title(Impact of treatment assignment on standardized test scores) pval keep( treatment)  addstat(permutation test, 0.038)
reg std_ENG treatment school* std_ENG_mock, vce(robust)
outreg2 using "${output}/table_A22.tex", dec(3) title(Impact of treatment assignment on standardized test scores) pval keep( treatment)  addstat(permutation test, 0.493)

**************************************
*	Table A24
**************************************

//fails each subject
foreach x of varlist MTC ENG CHEM BIO PHY HIST GEOG  {
capture tab `x', gen(`x'_)
}

preserve 
local i=1
foreach x of varlist MTC ENG {
tempfile tf`i' 
reg `x'_1 treatment school* ${controls} std_`x'_mock, vce(robust)
parmest, format(p %12.9f) saving(`tf`i'',replace) idn(`i')
local i=`i'+1
}
foreach x of varlist CHEM_1 BIO_1 PHY_1 HIST_1 GEOG_1 {
tempfile tf`i' 
reg `x' treatment school* ${controls} std_AGG_mock, vce(robust)
parmest, format(p %12.9f) saving(`tf`i'',replace) idn(`i')
local i=`i'+1
}

dsconcat `tf1' `tf2' `tf3'  `tf4'  `tf5'  `tf6'  `tf7'  
keep if parm=="treatment"
keep p idnum
qqvalue p, method(yekutieli) qvalue(q)
format q   %12.8fc
*qqvalue p, method(bonferroni) qvalue(q2)
foreach x of numlist 1/`i' {
global T1q`x'=q[`x']
display ${T1q`x'}
}
restore


local i=1
foreach x of varlist MTC ENG {
sum `x'_1 if treatment==0
local mean=r(mean)
reg `x'_1 treatment school* ${controls} std_`x'_mock, vce(robust)
if `i'==1 {
outreg2 using "${output}/table_A24.tex", dec(2) title(Impact of treatment on fail) keep(`x'_1 treatment ${controls} std_`x'_mock) adec(2) addstat(control mean, `mean', qvalue, ${T1q`i'}) pvalue replace
}
else {
outreg2 using "${output}/table_A24.tex", dec(2) title(Impact of treatment on fail) keep(`x'_1 treatment ${controls} std_`x'_mock) adec(2) addstat(control mean, `mean', qvalue, ${T1q`i'}) pvalue
}
local i=`i'+1
}

foreach x of varlist CHEM_1 BIO_1 PHY_1 HIST_1 GEOG_1 {
sum `x' if treatment==0
local mean=r(mean)
reg `x' treatment school* ${controls} std_AGG_mock, vce(robust)
outreg2 using "${output}/table_A24.tex", dec(2) title(Impact of treatment on fail) keep(`x' treatment ${controls} std_AGG_mock) adec(2) addstat(control mean, `mean', qvalue, ${T1q`i'}) pvalue
}

